package com.gsy.springai.repository;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@RequiredArgsConstructor
public class InMemoryChatHistoryRepository  implements ChatHistoryRepository {

    //使用一个map将type 和charIds进行存储

    private Map<String , List<String>> chatHistory = new HashMap<>();

    @Override
    public void save(String type, String chatId) {
        //如果之前没有相关的类型，则进行创建
        if (!chatHistory.containsKey(type)){
            chatHistory.put(type,new ArrayList<>());
        }
        //将按照类型将对话的列表拿出来，如果不包含chatId则加进去
        List<String> chatIds = chatHistory.get(type);
        if (!chatIds.contains(chatId)){
            chatIds.add(chatId);
        }else {
            return ;
        }
    }

    @Override
    public List<String> getChatIds(String type) {
        List<String> chatIds = chatHistory.get(type);

        return chatIds == null ? List.of() : chatIds;
    }
}
